//
//  MYGAcademicConferenceVC.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/18.
//

import UIKit
import JXSegmentedView

class MYGAcademicConferenceVC: MYGBaseController {
    var segmentedDataSource: JXSegmentedTitleDataSource = {
        let control = JXSegmentedTitleDataSource()
        control.titles = ["全部会议","我的会议"]
        control.isTitleColorGradientEnabled = true
        control.isTitleZoomEnabled = true
        control.titleNormalColor = .rgba(118, 118, 118, 1)
        control.titleSelectedColor = .rgba(32, 92, 255, 1)
        control.titleNormalFont = .regular(15)
        control.titleSelectedFont = .medium(18)
        return control
    }()
    
    var selectedIndex = 0
    lazy var layoutStyleBtn: UIButton = {
        let control = UIButton()
        control.setImage(UIImage.init(named: "Academic_style1"), for: .selected)
        control.setImage(UIImage.init(named: "Academic_style2"), for: .normal)
        control.addTarget(self, action: #selector(exchangeStyle(_:)), for: .touchUpInside)
        return control
    }()
    var line : UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "eeeeee")
        return control
    }()
    
    lazy var timeBtn:UIButton = {
        let control = UIButton()
        control.setTitle("时间", for: .normal)
        control.titleLabel?.font = .regular(14)
        control.setTitleColor(UIColor.init(hexString: "8D8D96"), for: .normal)
        control.setTitleColor(UIColor.init(hexString: "205CFF"), for: .selected)
        control.addTarget(self, action: #selector(tapTimeBtn(_:)), for: .touchUpInside)
        control.isSelected = true
        return control
    }()
    
    lazy var hotBtn:UIButton = {
        let control = UIButton()
        control.setTitle("热度", for: .normal)
        control.titleLabel?.font = .regular(14)
        control.setTitleColor(UIColor.init(hexString: "8D8D96"), for: .normal)
        control.setTitleColor(UIColor.init(hexString: "205CFF"), for: .selected)
        control.addTarget(self, action: #selector(tapHotBtn(_:)), for: .touchUpInside)
        return control
    }()
    
    lazy var segment:JXSegmentedView = {
         let control  = JXSegmentedView()
         control.delegate = self
         return control
     }()
    
    var is_time = ""
    var is_heat = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        configSegMent()
        self.title = "学术会议"

    }
    
    func configSegMent() {
        self.view.addSubview(segment)
        segment.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.height.equalTo(46.5)
            make.width.equalTo(200)
        }
        //关联dataSource
        segment.dataSource = self.segmentedDataSource
        
//        let indicator = JXSegmentedIndicatorLineView()
//        indicator.indicatorWidth = 20
//        indicator.indicatorColor = UIColor.init(hexString: "205CFF")
//        segment.indicators = [indicator]
        
        view.addSubview(layoutStyleBtn)
        layoutStyleBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalTo(segment.snp.centerY)
            make.size.equalTo(CGSize.init(width: 60, height: 46.5))
        }
        view.addSubview(line)
        line.snp.makeConstraints { make in
            make.top.equalTo(segment.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        view.addSubview(timeBtn)
        timeBtn.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.size.equalTo(CGSize.init(width: screenWidth/2, height: 45))
            make.left.equalToSuperview()
        }
        view.addSubview(hotBtn)
        hotBtn.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.size.equalTo(CGSize.init(width: screenWidth/2, height: 45))
            make.right.equalToSuperview()
        }
        
        let listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        listContainerView.snp.makeConstraints { make in
            make.top.equalTo(hotBtn.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        //关联listContainer
        segment.listContainer = listContainerView
        
    }

    @objc func exchangeStyle(_ sender:UIButton) {
        sender.isSelected = !sender.isSelected
        NotificationCenter.default.post(name: Notification.Name("MYGAcademicConferenceListVC_reloadStyle"), object: nil, userInfo: ["style": (sender.isSelected ? 2: 1),"tag":(100 + selectedIndex)])
    }
    
    @objc func tapTimeBtn(_ sender:UIButton) {
        hotBtn.isSelected = false
        timeBtn.isSelected = true
        NotificationCenter.default.post(name: Notification.Name("MYGAcademicConferenceListVC_refreshData"), object: nil, userInfo: ["is_time": "1","is_heat":"","tag":(100 + selectedIndex)])
    }
    @objc func tapHotBtn(_ sender:UIButton) {
        hotBtn.isSelected = true
        timeBtn.isSelected = false
        NotificationCenter.default.post(name: Notification.Name("MYGAcademicConferenceListVC_refreshData"), object: nil, userInfo: ["is_time": "","is_heat":"1","tag":(100 + selectedIndex)])
    }
}
extension MYGAcademicConferenceVC : JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.titles.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let vc = MYGAcademicConferenceListVC()
        vc.tag = 100 + index
        return vc
    }
   
}
extension MYGAcademicConferenceVC: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        if index == 1 {
            if ((MYGUserManager.getUser()?.doctor.name.isEmpty) == true) || MYGUserManager.getUser()?.doctor.gender == 0 || ((MYGUserManager.getUser()?.doctor.identity.isEmpty) == true) {
                segmentedView.selectItemAt(index: 0)
                let vc = MYGBaseInfoController()
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
                return
            }
            
            if MYGUserManager.isDoctorInfComplete  {
                if MYGUserManager.getUser()?.doctor.status == 20 {
                   
                }else {
                    segmentedView.selectItemAt(index: 0)
                    MMYGHUD.showToast(text: "正在审核中，暂时无法使用该功能")
                    return
                }
        
            }else {
                segmentedView.selectItemAt(index: 0)
                let vc = MYGUpdateInfoViewController()
                vc.model = MYGUserManager.getUser()!
                vc.isEdit = true
                UIViewController.currentViewController?.navigationController?.pushViewController(vc)
                return
            }
        }
        selectedIndex = index
        if index == 1{
           
            NotificationCenter.default.post(name: Notification.Name("MYGAcademicConferenceListVC_refreshData"), object: nil, userInfo: ["is_time": "","is_heat":"1","tag":(100 + selectedIndex)])

        }
    
    }
}
